package com.shiftl.data.aroma.dao;

import java.util.Collection;
import java.util.LinkedList;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;
import com.shiftl.data.aroma.entity.IngredientEnt;
import com.shiftl.data.jdo.ORMUtil;
import com.shiftl.data.jdo.PMF;

public class IngredientDAO {
	
	public IngredientEnt getIngredient(Long id){
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	        return pm.getObjectById(IngredientEnt.class, id);
	    } finally {
	        pm.close();
	    }
	}
	

	public Collection<IngredientEnt> getIngredientMulti(Collection<Key> keys){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Collection<String> ids = ORMUtil.keysToStrings(keys);
		Collection<IngredientEnt> ingrds = new LinkedList<IngredientEnt>();
		try {
//			for(String id: ids){
//				ingrds.add(pm.getObjectById(IngredientEnt.class,id));
//			}
			
			for(Key key: keys){
				ingrds.add(pm.getObjectById(IngredientEnt.class,key));
			}
	        return ingrds;
	    } finally {
	        pm.close();
	    }
		
	}
	
	public void saveIngredient(IngredientEnt ingredient){
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	        pm.makePersistent(ingredient);
	    } finally {
	        pm.close();
	    }
	}
    
}
